// pages/index/appraise-plate-detail/plate.js

const requestUtil = require("../../../utils/request");
const api = require("../../../config/api");
const common = require("../../../utils/common");
const util = require("../../../utils/util");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appraisalTabArray: [],
    AppraisalTab: 0,
    gradeList: [], // 年级列表
    gradeListIndex: 0, // 年级列表索引
    weekList: [],
    weekListIndex: 0,
    weekNo: 1, // 周no
    userInfo: null,
    isOnload: false,
    tableHeader: [{
      prop: 'grade',
      width: 130,
      label: '年级'
    }, {
      prop: 'clazz',
      width: 130,
      label: '班级'
    }, {
      prop: 'teacherName',
      width: 150,
      label: '班主任'
    }, {
      prop: 'weekRank',
      width: 150,
      label: '本周排名'
    }, {
      prop: 'weekScore',
      width: 150,
      label: '本周分数'
    }, {
      prop: 'termRank',
      width: 150,
      label: '学期排名'
    }, {
      prop: 'termScore',
      width: 150,
      label: '学期分数'
    }, {
      prop: 'detail',
      width: 150,
      label: '班级详情'
    }], // 板块负责人评比表头
    appraisalAdminTableList: [], // 板块负责人评比表格数据
    border: true,
    tableNoDataMsg: '暂无数据',
    isLeader: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = getApp().globalData.teacherInfo[0];
    let weekNo = options.weekNo;
    let isLeader = util.isNullOrEmpty(options.isLeader) ? false : true;
    this.setData({
      userInfo: userInfo,
      weekNo: weekNo,
      isLeader: isLeader,
      tabType: options.tabType,
      dutyType: options.dutyType,
      isOnload: true
    })
    this.getPlate();
  },
  onShow() {
    if(this.data.isOnload) this.getPlateItemTotal();
  },
  async getPlate() {
    let that = this;
    let res = await requestUtil.request("GET", api.getDictData + 'eva_class', null);
    let appraisalTabArray = [];
    for(let i in res.data.data) {
      appraisalTabArray.push(res.data.data[i].dictValue);
    }
    that.setData({
      appraisalTabArray: appraisalTabArray
    })
    this.getStudentOrganize();
  },
  /**
   * 获取年级列表
   */
  async getStudentOrganize() {
    let res = await requestUtil.request('GET', api.studentOrganize, {
      schoolId: this.data.userInfo.schoolId
    })
    let data = res.data.data;
    for (let i of data) {
      i.subList = null;
    }
    let obj = {
      gradeName: '全部',
      studyType: 1
    }
    data.unshift(obj);
    this.setData({
      gradeList: data
    });
    this.getWeekInfo();
  },

  /**
   * 获取周列表
   */
  async getWeekInfo() {
    //FIXME 其实这里用currDutyTeacher接口会简单点，但我懒得改了
    let res = await requestUtil.request("GET", api.dutyWeekList, {
      schoolId: this.data.userInfo.schoolId
    })
    let data = res.data.data;
    let weekList = [];
    let weekListIndex = 0;
    for (let i in data) {
      let startDate = common.getStringMonthAndDay(data[i].weekStartDate);
      let endDate = common.getStringMonthAndDay(data[i].weekEndDate);
      let str = '第' + data[i].weekNo + '周，' + startDate + ' - ' + endDate;
      let obj = {
        termId: data[i].termId,
        termName: data[i].termName,
        weekNo: data[i].weekNo,
        date: str
      }
      weekList.push(obj);
      if (data[i].weekNo == this.data.weekNo) {
        weekListIndex = i;
      }
    }
    this.setData({
      weekList,
      weekListIndex
    });
    this.getPlateItemTotal();
  },

  /**
   * tab选择
   */
  appraisalTabSelect(e) {
    this.setData({
      AppraisalTab: e.currentTarget.dataset.id
    })
    this.getPlateItemTotal();
  },

  /**
   * 年级列表选择
   */
  gradeListPickerChange(e) {
    this.setData({
      gradeListIndex: e.detail.value
    })
    this.getPlateItemTotal();
  },

  /**
   * 获取板块负责人的表格数据
   */
  async getPlateItemTotal() {
    let params = {
      schoolId: this.data.userInfo.schoolId,
      termId: this.data.weekList[this.data.weekListIndex].termId,
      weekNo: this.data.weekNo,
      gradeName: this.data.gradeList[this.data.gradeListIndex].gradeName == '全部' ? null : this.data.gradeList[this.data.gradeListIndex].gradeName,
      plateType: this.data.appraisalTabArray[this.data.AppraisalTab]
    }
    let res = await requestUtil.request("POST", api.plateItemTotal, params);
    let data = res.data.data;
    let list = [];
    for (let i of data) {
      let obj = {
        grade: i.gradeName,
        weekRank: '第' + i.weekRank + '名',
        weekScore: i.weekScore + '分',
        teacherName: i.teacherName,
        clazz: i.clazz + '班',
        termRank: '第' + i.termRank + '名',
        termScore: i.termScore + '分',
        detail: '详情'
      }
      list.push(obj);
    }
    this.setData({
      tableList: list
    })
  },

  /**
   * 跳转到班级详情
   */
  clazzDetail() {
    let weekNo = this.data.weekNo;
    let plateType = this.data.appraisalTabArray[this.data.AppraisalTab];
    if (this.data.isLeader) {
      wx.navigateTo({
        url: '../appraise-clazz-detail/clazz?evaType=1&weekNo=' + weekNo + '&plateType=' + plateType + '&isLeader=' + this.data.isLeader +
        '&tabType=' + this.data.tabType +
        '&dutyType=' + this.data.dutyType,
      })
    } else {
      wx.navigateTo({
        url: '../appraise-clazz-detail/clazz?evaType=1&weekNo=' + weekNo + '&plateType=' + plateType +
        '&tabType=' + this.data.tabType +
        '&dutyType=' + this.data.dutyType,
      })
    }
  },

  /**
   * 行点击跳转到班级详情页
   */
  onRowClick(e) {
    let weekNo = this.data.weekNo;
    let clazz = e.detail.currentTarget.dataset.it.clazz;
    clazz = clazz.substr(0, clazz.length - 1);
    let gradeName = e.detail.currentTarget.dataset.it.grade;
    let plateType = this.data.appraisalTabArray[this.data.AppraisalTab];
    if (this.data.isLeader) {
      wx.navigateTo({
        url: '../appraise-clazz-detail/clazz?evaType=1&clazz=' + clazz + '&gradeName=' + gradeName + '&weekNo=' + weekNo + '&plateType=' + plateType + '&isLeader=' + this.data.isLeader +
        '&tabType=' + this.data.tabType +
        '&dutyType=' + this.data.dutyType,
      })
    } else {
      wx.navigateTo({
        url: '../appraise-clazz-detail/clazz?evaType=1&clazz=' + clazz + '&gradeName=' + gradeName + '&weekNo=' + weekNo + '&plateType=' + plateType +
        '&tabType=' + this.data.tabType +
        '&dutyType=' + this.data.dutyType,
      })
    }
  },

  /**
   * 星期列表选择
   */
  weekListPickerChange(e) {
    this.setData({
      weekListIndex: e.detail.value,
      weekNo: this.data.weekList[e.detail.value].weekNo
    })
    this.getPlateItemTotal();
  },
})